$(function () {
    Table.builder()
        .listUrl(STAION_ALARM_RECORD_LIST_ALL)
        .init();

    $("#tableAjaxId").on("click", ".updateStatus", function () {
        var id = $.trim($(this).attr("dataId"));
        Request.builder()
            .url(STAION_ALARM_RECORD_DETAIL + "/" + id)
            .isToken(true)
            .callback(function (e) {
                if (e.code === "00000") {
                    var data = e.data;
                    Form.setValues($("#updateStatus_form"), data)
                    document.getElementById("dataId").value = id;
                } else {
                    LayerUtil.failMsg(e.msg != null ? e.msg : ERROR_MSG.FAIL)
                }
            }).get();
    })

    $("#updateStatusBtn").click(function () {
        var data = Form.getValues($("#updateStatus_form"))
        Request.builder()
            .url(STAION_ALARM_RECORD_UPDATE_STATUS + "/" + data.dataId)
            .data({'id': data.dataId, 'lastModifyTime': data.lastModifyTime})
            .isToken(true)
            .callback(function (e) {
                if (e.code === "00000") {
                    LayerUtil.successMsg(ERROR_MSG.SUCCESS)
                    setTimeout(function () {
                        $("#tableAjaxId").bootstrapTable('refresh', {pageNumber: 1});
                        $("#updateStatusDiv").find(".close").trigger("click");
                    }, 1000)
                } else {
                    LayerUtil.failMsg(e.msg != null ? e.msg : ERROR_MSG.FAIL)
                }
                $(".mx_validator_button").attr("disabled", false)
            }).post();
    })
});

columns = [
    {
        field: 'stationName', 
        title: '设备名称', 
        align: 'center', 
        valign: 'middle' 
    }, {
        field: 'status', 
        title: '状态', 
        align: 'center', 
        valign: 'middle',
        formatter: function (value, row, index) { 
            if (value === 0) {
                return "<span style='color:red'>报警中</span>";
            } else if (value === 1) {
                return "<span style='color:green'>已消除</span>";
            } else {
                return "未知";
            }
        }
    }, {
        field: 'type', 
        title: '报警类别', 
        align: 'center', 
        valign: 'middle',
        formatter: function (value, row, index) { 
            if (value === 0) {
                return "离线";
            } else if (value === 1) {
                return "维保到期";
            } else if (value === 2) {
                return "温度越界";
            } else if (value === 3) {
                return "超时报警";
            } else if (value === 4) {
                return "饲料空缺";
            } else if (value === 5) {
                return "其他";
            } else {
                return "未知";
            }
        }
    }, {
        field: 'content', 
        title: '警报内容', 
        align: 'center', 
        valign: 'middle'
    }, {
        field: 'createTime',
        title: '发生时间',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) { 
            return DateUtil.changeDateToString(value, DateFormatEnum.YYYY_MM_DD_HH_MM);
        }
    }, {
        field: 'endTime',
        title: '结束时间',
        align: 'center',
        valign: 'middle',
        formatter: function (value, row, index) { 
            if (value === 0) {
                return "";
            }
            return DateUtil.changeDateToString(value, DateFormatEnum.YYYY_MM_DD_HH_MM);
        }
    }, {
        title: "操作",
        align: 'center',
        valign: 'middle',
        width: 100, 
        formatter: option
    }
]

function option(value, row, index) {
    var html = '<div class="d-flex justify-content-center">';
    if (row.type === 3) {
        html +=  '<a class="btn btn-xs btn-default updateStatus" data-toggle="modal" data-target="#updateStatusDiv" dataId="' + row.id + '" title="消除报警" >消除报警</a>';
    }
    return html;
}